import re

import requests
import  asyncio
import aiohttp
import json
# async def fetch(session,url):
#     async with session.get(url) as response:
#         text = await response.text()
#         json_str = re.search(r'\((.*)\)',text).group(1)
#         data = json.loads(json_str)
#         print(data)
#         return data
# async def main():
#     async with aiohttp.ClientSession() as session:
#         urls = [f'https://push2.eastmoney.com/api/qt/clist/get?np=1&fltt=1&invt=2&fs=m%3A0%2Bt%3A6%2Bf%3A!2%2Cm%3A0%2Bt%3A80%2Bf%3A!2%2Cm%3A1%2Bt%3A2%2Bf%3A!2%2Cm%3A1%2Bt%3A23%2Bf%3A!2%2Cm%3A0%2Bt%3A81%2Bs%3A262144%2Bf%3A!2&fields=f12%2Cf13%2Cf14%2Cf1%2Cf2%2Cf4%2Cf3%2Cf152%2Cf5%2Cf6%2Cf7%2Cf15%2Cf18%2Cf16%2Cf17%2Cf10%2Cf8%2Cf9%2Cf23&fid=f3&pn={page}'for page in range(1, 5)]
#         tasks = [fetch(session,url) for url in urls]
#         await asyncio.gather(*tasks)
# asyncio.run(main())


headers = {
    "accept": "*/*",
    "accept-encoding": "gzip, deflate, zstd",
    "accept-language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7",
    "cache-control": "no-cache",
    "connection": "keep-alive",
    "cookie": "fullscreengg=1; fullscreengg2=1; qgqp_b_id=e7d923606642f5037ab13d39c3223e43; st_nvi=btbcXXaNZ9WaIUWi0Xpbi5093; st_si=27977450321530; st_pvi=71579644274743; st_sp=2025-11-04%2014%3A06%3A47; st_inirUrl=; st_sn=1; st_psi=20251104140647586-113200301321-7667011268; st_asi=delete; nid=03f3559e8c48d0db59fc7014564e7b90; nid_create_time=1762236407992; gvi=Hj0sMUGujhKDybp2dBfKG2d76; gvi_create_time=1762236407992",
    "host": "push2.eastmoney.com",
    "pragma": "no-cache",
    "referer": "https://quote.eastmoney.com/center/gridlist.html",
    "sec-ch-ua": "\"Chromium\";v=\"142\", \"Google Chrome\";v=\"142\", \"Not_A Brand\";v=\"99\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "\"Windows\"",
    "sec-fetch-dest": "script",
    "sec-fetch-mode": "no-cors",
    "sec-fetch-site": "same-site",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36"
}


# 同步模式
# urls = [f'https://push2.eastmoney.com/api/qt/clist/get?np=1&fltt=1&invt=2&fs=m%3A0%2Bt%3A6%2Bf%3A!2%2Cm%3A0%2Bt%3A80%2Bf%3A!2%2Cm%3A1%2Bt%3A2%2Bf%3A!2%2Cm%3A1%2Bt%3A23%2Bf%3A!2%2Cm%3A0%2Bt%3A81%2Bs%3A262144%2Bf%3A!2&fields=f12%2Cf13%2Cf14%2Cf1%2Cf2%2Cf4%2Cf3%2Cf152%2Cf5%2Cf6%2Cf7%2Cf15%2Cf18%2Cf16%2Cf17%2Cf10%2Cf8%2Cf9%2Cf23&fid=f3&pn={page}&pz=20&po=1&dect=1&ut=fa5fd1943c7b386f172d6893dbfba10b&wbp2u=%7C0%7C0%7C0%7Cweb&_=1762242291108'for page in range(1,274)]
# for url in urls:
#     res = requests.get(url)
#     response = res.json()['data']['diff']
#     for item in response:
#         print(item['f14'])





# 异步解决
CONCURRENCY = 4
DELAY = 0.5          # 每页间隔
sem   = asyncio.Semaphore(CONCURRENCY)


async def fetch(session, url):
    async with sem:
        async with session.get(url,headers=headers) as resp:
            text = await resp.text()
            m = re.search(r'\((.*)\)', text)
            if not m:
                print('no match:', text)
                return {}
            return json.loads(m.group(1))

async def main():
    async with aiohttp.ClientSession() as session:
        urls = [f'https://push2.eastmoney.com/api/qt/clist/get?np=1&fltt=1&invt=2&fs=m%3A0%2Bt%3A6%2Bf%3A!2%2Cm%3A0%2Bt%3A80%2Bf%3A!2%2Cm%3A1%2Bt%3A2%2Bf%3A!2%2Cm%3A1%2Bt%3A23%2Bf%3A!2%2Cm%3A0%2Bt%3A81%2Bs%3A262144%2Bf%3A!2&fields=f12%2Cf13%2Cf14%2Cf1%2Cf2%2Cf4%2Cf3%2Cf152%2Cf5%2Cf6%2Cf7%2Cf15%2Cf18%2Cf16%2Cf17%2Cf10%2Cf8%2Cf9%2Cf23&fid=f3&pn={page}&pz=20&po=1&dect=1&ut=fa5fd1943c7b386f172d6893dbfba10b&wbp2u=%7C0%7C0%7C0%7Cweb&_=1762242291108'for page in range(1, 274)]
        for i in range(0, len(urls), CONCURRENCY):
            tasks = [fetch(session, u) for u in urls[i:i+CONCURRENCY]]
            await asyncio.gather(*tasks)
            await asyncio.sleep(DELAY)
asyncio.run(main())





